 //#define BLINKER_WIFI
#include <ArduinoJson.h>
//#include <Blinker.h>
#include <NTPClient.h> 
#include <WiFiUdp.h> 
#include "display.h"
#include "lv_port_indev.h"
#include "lv_demo_encoder.h"
#include "imu.h"
#include "network.h"

Display screen;
IMU mpu;
Network networ;  
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp.aliyun.com"); 





//char auth[] = "78a9d46bc0f1";
char ssid[] = "@PHICOMM_5B";
char pswd[] = "f13828704732";

int32_t encoder_diff;
lv_indev_state_t encoder_state;

String  key = "Sd8al_0pDThjCwtJG"; //填写天气秘钥



#define WEATHER_PAGE_SIZE 2
int clock_page = 0;                                                  
unsigned long preWeatherMillis = 0;           // 上一回更新天气时的毫秒数
unsigned long preTimeMillis = 0;              // 上一回从网络更新日期与时间时的毫秒数
unsigned long weatherUpdataInterval = 20000; // 天气更新的时间间隔
unsigned long timeUpdataInterval = 2000;  



Imu_Action *actnfo;







void setup(){   
  
    Serial.begin(115200);
    //Blinker.begin(auth, ssid, pswd);

    networ.init(ssid,pswd);
    delay(1000);
    
    
     
    screen.init();
    lv_port_indev_init();
    
    mpu.init();
    weather_init();
    timeClient.begin();
    timeClient.setTimeOffset(28800);  
    
    Serial.println(networ.getBilibiliFans("http://api.bilibili.com/x/relation/stat?vmid=20259914"));

    actnfo = mpu.update(200);
     lv_scr_load_anim(wc_scr[0],LV_SCR_LOAD_ANIM_MOVE_LEFT, 300, 300, false); 
} 
       
      


void loop(){
  networ.getwifi();
   timeClient.update();
   
   actnfo = mpu.update(200);
   weather_process(actnfo);
    
    screen.routine();
 
       
 
      
     
      }
      


void UpdateWeather()
{   
  String timeString,dateString;
  int weed;
  unsigned long epochTime = timeClient.getEpochTime();
  int tian;
  int faly;
  String tiangi;
  String fayl;
  if (millis() - preWeatherMillis >= weatherUpdataInterval)
     {
     Weather mweather;
      faly = networ.getBilibiliFans("http://api.bilibili.com/x/relation/stat?vmid=20259914");
      mweather = networ.getTianqi("https://api.seniverse.com/v3/weather/now.json?key=Sd8al_0pDThjCwtJG&location=Dongguan&language=zh-Hans&unit=c");
    tiangi = String(mweather.temperature);
    fayl = String(faly);   
    //Serial.println(tian);  
   
     
         timeString = String(timeClient.getHours()) + ":" + adjDigit(timeClient.getMinutes());
         struct tm *ptm = gmtime ((time_t *)&epochTime);
         int monthDay = ptm->tm_mday;
         int tm_Month = ptm->tm_mon+1;
         int tm_Year = ptm->tm_year+1900;
         dateString = adjDigit(tm_Year) + "-" + adjDigit(tm_Month)+ "-"+adjDigit(monthDay);
          weed =  timeClient.getDay();
          }
     display_weather_time(fayl.c_str(), tiangi.c_str(),timeString.c_str(),dateString.c_str(), weed);
       
    
}


        

        

//void Upmood(){
//  
//        int weed =  timeClient.getDay();
//
//       display_mood(weed);
//         
//  }

String adjDigit(int number){
  if (number >= 10){
    return String(number);
  } else {
    return String("0" + String(number));
  }
}


    

void weather_process(const Imu_Action *act_info){
    lv_scr_load_anim_t anim_type = LV_SCR_LOAD_ANIM_NONE;
  
    if (TURN_RIGHT == actnfo->active)
    {
        // 切换界面时，变相强制更新
        preWeatherMillis = millis() - weatherUpdataInterval;
        preTimeMillis = millis() - timeUpdataInterval;
        anim_type = LV_SCR_LOAD_ANIM_MOVE_RIGHT;
        clock_page =(clock_page + 1) % WEATHER_PAGE_SIZE;
        
    }
    else if (TURN_LEFT == actnfo->active)
    {
        // 切换界面时，变相强制更新
        preWeatherMillis = millis() - weatherUpdataInterval;
        preTimeMillis = millis() - timeUpdataInterval;
        anim_type = LV_SCR_LOAD_ANIM_MOVE_LEFT;
        // 以下等效与 clock_page = (clock_page + WEATHER_PAGE_SIZE - 1) % WEATHER_PAGE_SIZE;
        // +3为了不让数据溢出成负数，而导致取模逻辑错误
        clock_page = (clock_page + WEATHER_PAGE_SIZE - 1) % WEATHER_PAGE_SIZE;
         
    }
      
   
    if ( 0 == clock_page) // 更新天气
    { 
     
      UpdateWeather();
       
       
    }

  if (1 == clock_page) // 更新时钟
    {   
       
       
         
    }
     
}
 

void weather_init(void)
{
    weather_gui_init();
    
    preWeatherMillis = millis() - weatherUpdataInterval;
    preTimeMillis = millis() - timeUpdataInterval;
} 





 
